#include <iostream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>

/*
  16.3 (1.5*) Using the vector from ex16-2, write a loop to print the names
  of all fruits with the initial letter a.

  16.4 (1.5*) write a loop to delete all fruits with the initial
  letter a.
*/

// I cannot use class here. WHY?
struct initial : std::unary_function<std::string, bool>
{
  initial(char __c)
    : c(__c)
  { }

  bool
  operator ()(const std::string& s) const
  {
    return (s[0] == c);
  }

  char c;
};

int main()
{
  std::vector<std::string> fruits;

  std::istream_iterator<std::string> begin_of_cin(std::cin);
  std::istream_iterator<std::string> end_of_cin;
  std::back_insert_iterator<std::vector<std::string> > dest(fruits);

  std::copy(begin_of_cin, end_of_cin, dest);

  std::cout << "Input:" << std::endl;
  std::copy(fruits.begin(), fruits.end(), std::ostream_iterator<std::string>(std::cout, " "));
  std::cout << std::endl;

  std::sort(fruits.begin(), fruits.end());

  std::cout << "After sorting:" << std::endl;
  std::copy(fruits.begin(), fruits.end(), std::ostream_iterator<std::string>(std::cout, " "));
  std::cout << std::endl;

  std::cout << "Fruits with the initial letter \'a\':" << std::endl;
  std::vector<std::string>::iterator i1 = std::find_if(fruits.begin(), fruits.end(), initial('a'));
  std::vector<std::string>::iterator i2 = std::find_if(i1, fruits.end(), std::not1 (initial('a')));

  std::copy(i1, i2, std::ostream_iterator<std::string>(std::cout, " "));
  std::cout << std::endl;

  std::cout << "After deleting all fruits with initial letter \'a\':" << std::endl;
  fruits.erase(i1, i2);
  std::copy(fruits.begin(), fruits.end(), std::ostream_iterator<std::string>(std::cout, " "));
  std::cout << std::endl;
}
